package by.amakarevich.webapp.page.shoppingcart.panel;

import javax.inject.Inject;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.resource.UrlResourceReference;

import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.ShoppingCartInfo;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.datamodel.enums.Category;
import by.amakarevich.services.ShoppingCartInfoService;
import by.amakarevich.services.ShoppingCartService;
import by.amakarevich.webapp.app.BasicAuthenticationSession;
import by.amakarevich.webapp.page.shoppingcart.ShoppingCartPage;

public class ShoppingCartInfoPanel extends Panel {

	@Inject
	private ShoppingCartInfoService shoppingCartInfoService;
	@Inject
	private ShoppingCartService shoppingCartService;

	private ShoppingCartInfo shoppingCartInfo;
	private Product product;

	UserAccount user = BasicAuthenticationSession.get().getUser();

	ShoppingCart shoppingCart = shoppingCartService.getByUser(user);

	public ShoppingCartInfoPanel(String id, Long shoppingCartInfoId) {
		super(id);
		shoppingCartInfo = shoppingCartInfoService.getByShoppingCartId(shoppingCartInfoId);
	}

	public ShoppingCartInfoPanel(String id, ShoppingCartInfo shoppingCartInfo) {
		super(id);
		this.shoppingCartInfo = shoppingCartInfo;
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		WebMarkupContainer liContainer = new WebMarkupContainer("wrapper-li");
		add(liContainer);

		WebMarkupContainer hContaienr = new WebMarkupContainer("wrapper-h");
		liContainer.add(hContaienr);

		shoppingCartInfo = shoppingCartInfoService.getByShoppingCartId(shoppingCartInfo.getId());

		product = shoppingCartInfo.getProduct();
		hContaienr.add(new Label("item", new Model<String>(product.getItem())));
		String urlStr="http://localhost:8081/imagesService/"+product.getId().toString()+".jpg";
		final UrlResourceReference url= new UrlResourceReference(Url.parse(urlStr));
		liContainer.add(new Image("img", url).setVisible(!product.getCategory().equals(Category.thread)));
		liContainer.add(new Label("company", new Model<String>(product.getCompany())));
		liContainer.add(new Label("quantity", new Model<Long>(shoppingCartInfo.getQuantity())));
		liContainer.add(new Label("price", new Model<Long>(shoppingCartInfo.getPrice())));

		add(new Link("delete-product-shopping-cart-link") {
			@Override
			public void onClick() {
				if(shoppingCartInfo.getQuantity()==1l){
					shoppingCartInfoService.deleteShoppingCartInfo(shoppingCartInfo.getId());					
					}else{
						shoppingCartInfo.setQuantity(shoppingCartInfo.getQuantity()-1l);
						shoppingCartInfoService.updateShoppingCartInfo(shoppingCartInfo);
					}
				setResponsePage(new ShoppingCartPage());
			}
		});
	}

}
